<?php
class common extends main{
	
	//COMMON
	public static function getContent($filename) {
            $content = @file_get_contents($filename)or die(error::show("core1",__LINE__,__FILE__,'fichier : '.$filename));
            return $content;
        }
	
	//MAILS
	
	public static function envois_mail($dest,$sujet,$message) {
		$headers = "From:". parent::getConfig("site_name") ."\n";
		$headers .= 'X-Mailer: PHP/' . phpversion() . '\n';
		$headers .= "MIME-version: 1.0\n";
		$headers .= "Content-type: text/html; charset= UTF-8\n";
		$headers .= "Content-Transfer-Encoding: ­ 8bit\n";
		$mail = $message;
		if(mail($dest,$sujet,$mail,$headers)){return true;}
		else{return false;}
	}
	
	//FILE/DIRECTORY MANIPULATION
	
	public static function recurse_copy($src,$dst) {
		$dir = opendir($src);
		@mkdir($dst);
		while(false !== ( $file = readdir($dir)) ) {
		    if (( $file != '.' ) && ( $file != '..' )) {
			if ( is_dir($src . '/' . $file) ) {
			    recurse_copy($src . '/' . $file,$dst . '/' . $file);
			}
			else {
			    copy($src . '/' . $file,$dst . '/' . $file);
			}
		    }
		}
		closedir($dir);
	}
	
	public static function create_dir($dir,$chmod = 0775) {
		if(mkdir($dir,$chmod)){
		    chmod($dir,$chmod);
		    //chown($dir, parent::getConfig('owner'));
		    //chgrp($dir, parent::getConfig('group'));
		    return true;
		}else{
		    return false;
		}
	}
	
	//TAGS/TEMPLATE
	
	//Fusion de 2 tableaux de tags
	public static function tags_merge($tags1, $tags2) {
		$tags = array();
		if(is_array($tags1)&& is_array($tags2)){
			foreach($tags1 as $key=>$value){
				$tags[$key] = $value.$tags2[$key];
				if(isset($tags2[$key]))
					unset($tags2[$key]);
			}
			
			$tags = array_merge($tags,$tags2);
		}else{
			$tags = $tags1;
		}
		
		return $tags;
	}
	
	//Implantation de tags dans un template
	protected static function mergeTagsTemplate($template, $tags){
		if(count($tags) > 0){
		    //Recupération du template
		    $page = $template;
		    
		    //D'abord on cherche les tags conditionnel :
		    //{$var:"value"|"value2"} => si $var(bool) dans ce cas affiche value sinon value2
		    //$page = self::mergeConditionnalTagsTemplate($page,$tags);
		    
		    foreach($tags as $tagName=>$contents){
			    if(is_array($contents)){
				    foreach($contents as $tag=>$content){
					$page = str_replace("{".$tagName.":".$tag."}" , $content, $page);
				    }
			    }else{
				    $page = str_replace("{".$tagName."}" , $contents, $page);
			    }
		    }
		}
		    
		$page = preg_replace('#(\{[a-zA-Z0-9_:]{3,}\})#U', '', $page);
		
		return $page;
	}
	
	public static function mergeTagsModuleTemplate($template, $tags, $moduleUrlName){
	    //Common tags
            $tags['page'] = main::getCommonTags();
            $tags['lang'] = lang::getTags();
            $tags['modlang'] = lang::getModTags($moduleUrlName);//Langue module
	    
	    if(count($tags) > 0){
		//Recupération du template
		$page = $template;
		
		//D'abord on cherche les tags conditionnel :
		//{$var:"value"|"value2"} => si $var(bool) dans ce cas affiche value sinon value2
		//$page = self::mergeConditionnalTagsTemplate($page,$tags);
		
		foreach($tags as $tagName=>$contents){
			if(is_array($contents)){
				foreach($contents as $tag=>$content){
				    $page = str_replace("{".$tagName.":".$tag."}" , $content, $page);
				}
			}else{
				$page = str_replace("{".$tagName."}" , $contents, $page);
			}
		}
	    }
		
	    $page = preg_replace('#(\{[a-zA-Z0-9_:]{3,}\})#U', '', $page);
	    
	    return $page;
	}
	
	
	// fonction privé qui cherche et remplace les tags conditionnels
	private function mergeConditionnalTagsTemplate($page,$tags){
	    //FIXME
	    if(preg_match("#{\$([a-z0-9_-]+)\:([a-z0-9_-]+|[\"|'][a-zA-Z0-9_-.@]+[\"|'])\|([a-z0-9_-]+|[\"|'][a-zA-Z0-9_-.@]+[\"|'])}#Ui",$page)){
		//TODO
	    }
	}
	
	
	// IP and HOST
	
	//Fonction permettant de récupérer l'IP
	public function getIp() {
		if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
		} else if(isset($_SERVER['HTTP_CLIENT_IP'])) {
			$ip  = $_SERVER['HTTP_CLIENT_IP'];
		} else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
	
	//Fonction permettant de récupérer le nom d'hôte
	public function getHost($ip) {
		return gethostbyaddr($ip);
	}
	
	//RANDOMIZATION
	
	//Fonction générant un code aléatoire servant de salt ou autre
	// $len = Longueur du code à générer
	// $list = Caractères autorisés pour la génération
	public function GenCode($len = 10, $list = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") {
	    $str = "";
	    for ($i=0;$i<$len;$i++) {$str .= $list[rand(0, (strlen($list)-1))];}
	    return $str;
	}
	
	
	//ESCAPE INTO STRINGS
	
	//Function qui echape les apostrophes (quote ')
	public function escapeQuote($str){
	    return str_replace("'", "\\'", $str);
	}
	
	//Function qui echape les guillements (doublequote ")
	public function escapeDoubleQuote($str){
	    return str_replace('"', '\\"', $str);
	}

};
?>